package com.test.controller;

import com.test.common.Result;
import com.test.common.ResultCodeEnum;
import com.test.util.WebUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.lang.reflect.Method;

@SuppressWarnings("all")

// controller层所有Servlet的父类，通过Java反射合并Servlet
public class BaseController extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String[] split = requestURI.split("/");
        String methodName = split[split.length - 1];
        // 通过反射获取要执行的方法
        Class aClass = this.getClass();
        try {
            Method method = aClass.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            // 设置方法可以访问
            method.setAccessible(true);
            // 通过反射执行代码
            method.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回错误响应
            Result result = Result.build(null, ResultCodeEnum.SYSTEM_ERROR);
            WebUtil.writeJson(resp, result);
        }
    }
}
